package com.zisuyi.uilibrary.module.dialog.rating

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.weilele.mvvm.utils.vLaout.VLayoutAdapter
import com.weilele.mvvm.utils.vLaout.VLayoutHolder
import com.zisuyi.uilibrary.databinding.RcvRatingBarBinding

class RatingBarAdapter : VLayoutAdapter<Int?,RatingBarAdapter.RatingBarHolder>() {
    private var currentNum = 0
    private var drawableNormal: Int = 0
    private var drawableNull: Int? = null
    init {
        setOnInterceptCreateViewHolderListener { self, parent, viewType ->
            val binding=RcvRatingBarBinding.inflate(LayoutInflater.from(parent.context),parent,false)
            RatingBarAdapter.RatingBarHolder(binding)
        }
    }
    fun setMax(maxNum: Int, @DrawableRes drawableNormal: Int, @DrawableRes drawableNull: Int?) {
        this.drawableNormal=drawableNormal
        this.drawableNull=drawableNull
        currentNum=maxNum
        val ratingList = mutableListOf<Int?>()
        repeat(maxNum) {
            if (it < currentNum) {
                ratingList.add(drawableNormal)
            } else {
                ratingList.add(drawableNull)
            }
        }
        refresh(ratingList)
    }

    fun setRating(num: Int) {
        currentNum = num
        val ratingList = mutableListOf<Int?>()
        repeat(datas.count()) {
            if (it < currentNum) {
                ratingList.add(drawableNormal)
            } else {
                ratingList.add(drawableNull)
            }
        }
        refresh(ratingList)
    }



    class RatingBarHolder: VLayoutHolder<Int?, RcvRatingBarBinding> {
        constructor(binding: RcvRatingBarBinding) : super(binding)
        constructor(itemView: View) : super(itemView)

        override fun bindData(drawableRes: Int?) {
            (itemView as ImageView).apply {
                if (drawableRes == null) {
                    setImageDrawable(null)
                } else {
                    setImageResource(drawableRes)
                }
            }
        }
    }
}

